package com.weichen.studentgameexchange.game.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.common.exception.ServiceException;
import com.weichen.studentgameexchange.game.dao.GameVersionMapper;
import com.weichen.studentgameexchange.game.entity.Game;
import com.weichen.studentgameexchange.game.entity.GameVersion;
import com.weichen.studentgameexchange.game.entity.vo.VersionVo;
import com.weichen.studentgameexchange.game.service.IGameVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-01
 */
@Service
public class GameVersionServiceImpl extends ServiceImpl<GameVersionMapper, GameVersion> implements IGameVersionService {

    @Autowired
    GameVersionMapper gameVersionMapper;

    /**
     * 新增游戏版本
     *
     * @param gameVersion
     */
    @Override
    public Result adminAdd(VersionVo versionVo) {
        String versionNumber = versionVo.getVersionNumber();
        String gameName = versionVo.getGameName();
        Game game = Db.lambdaQuery(Game.class)
                .eq(Game::getGameName, gameName)
                .one();
        if (game == null)
            return Result.error("游戏不存在！");
        GameVersion one = Db.lambdaQuery(GameVersion.class)
                .eq(versionNumber != null, GameVersion::getVersionNumber, versionNumber)
                .eq(GameVersion::getGameId, game.getGameId())
                .one();
        if (one != null)
            return Result.error("当前游戏版本已存在！");
        GameVersion gameVersion = BeanUtil.copyProperties(versionVo, GameVersion.class);
        gameVersion.setGameId(game.getGameId());
        return Result.success(gameVersionMapper.insert(gameVersion));
    }

    /**
     * 分页查询
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result adminPage(String keyword, Integer page, Integer size) {
        Page<GameVersion> gameVersionPage = new Page<>(page, size);
        LambdaQueryWrapper<GameVersion> gameVersionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        gameVersionLambdaQueryWrapper.orderByDesc(GameVersion::getVersionId);
        List<Game> games = Db.lambdaQuery(Game.class)
                .like(keyword != null, Game::getGameName, keyword)
                .list();
        if (games.size() > 0) {
            List<Long> gameIds = games.stream().map(Game::getGameId).toList();
            gameIds.forEach(gameId -> {
                gameVersionLambdaQueryWrapper.or().like(GameVersion::getGameId, gameId);
            });
        }
        Page<GameVersion> selectPage = gameVersionMapper.selectPage(gameVersionPage, gameVersionLambdaQueryWrapper);
        List<VersionVo> voList = new ArrayList<>();
        List<VersionVo> vos = selectPage.getRecords().stream().map(gameVersion -> {
            VersionVo vo = BeanUtil.copyProperties(gameVersion, VersionVo.class);
            String versionNumber = vo.getVersionNumber();
//                    String[] split = versionNumber.split(":");
//                    String name = null;
//                    if (split.length > 1) {
//                        name = split[1];
//                    }
            vo.setVersionNumber(versionNumber);
            Game game = Db.lambdaQuery(Game.class)
                    .eq(gameVersion.getGameId() != null, Game::getGameId, gameVersion.getGameId())
                    .one();
            if (game != null)
                vo.setGameName(game.getGameName());
            voList.add(vo);
            return vo;
        }).toList();
        Page<VersionVo> voPage = new Page<>();
        voPage.setCurrent(selectPage.getCurrent());
        voPage.setSize(selectPage.getSize());
        voPage.setTotal(selectPage.getTotal());
        voPage.setRecords(voList);
        return Result.success(voPage);
    }

    /**
     * 查询单个游戏版本
     *
     * @param versionId
     * @return
     */
    @Override
    public Result adminSelectOne(Long versionId) {
        GameVersion gameVersion = Db.lambdaQuery(GameVersion.class)
                .eq(GameVersion::getVersionId, versionId)
                .one();
        if (gameVersion == null)
            return Result.error("游戏版本不存在！");
        String versionNumber = gameVersion.getVersionNumber();
//        String[] split = versionNumber.split(":");
//        String name = split[1];
        gameVersion.setVersionNumber(versionNumber);
        Game game = Db.lambdaQuery(Game.class)
                .eq(gameVersion.getGameId() != null, Game::getGameId, gameVersion.getGameId())
                .one();
        if (game == null)
            return Result.error("游戏不存在！");
        VersionVo versionVo = BeanUtil.copyProperties(gameVersion, VersionVo.class);
        versionVo.setGameName(game.getGameName());
        return Result.success(versionVo);
    }

    /**
     * 查询所有游戏版本信息
     *
     * @param versionId
     * @return
     */
    @Override
    public List<GameVersion> adminSelectAll(Long versionId) {
        GameVersion gameVersion = Db.lambdaQuery(GameVersion.class)
                .eq(GameVersion::getVersionId, versionId)
                .one();
        Long gameId = gameVersion.getGameId();
        List<GameVersion> gameVersions = Db.lambdaQuery(GameVersion.class)
                .eq(GameVersion::getGameId, gameId)
                .list();
        Stream<GameVersion> gameVersionStream = gameVersions.stream().map(version -> {
            String versionNumber = version.getVersionNumber();
            String[] split = versionNumber.split(":");
            String name = split[1];
            version.setVersionNumber(name);
            return version;
        });
        return null;
    }

}
